<template>
    <div>
        <!--<div>用户code：{{code}}</div>-->
        <div style="text-align: center;margin-top: 10%;">用户登录...</div>
    </div>
</template>

<script>
    export default {
        name: "resident",
        data(){
            return{
                appid: 'wxde6aa775e08fa72c',
                code: '',
            }
        },
        methods:{
            loadopen() {
                this.loading = this.$loading({lock: true,text: 'Loading',spinner: 'el-icon-loading',background: 'rgba(0, 0, 0, 0.2)'});
                setTimeout(() => {this.loading.close();}, 5000);
            },
            loadclose() {this.loading.close();},
            GetRequest() {
                var url = location.href; //获取url中"?"符后的字串
                var theRequest = new Object();
                if (url.indexOf("?") != -1) {
                    // var str = url.substr(1);
                    var str = url.split('?')[1];
                    let strs = str.split("&");
                    for(var i = 0; i < strs.length; i ++) {
                        theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
                    }
                }
                return theRequest;
            },
            getAuthor() {
                var appid = this.appid, redirect_uri = encodeURIComponent('http://www.dlhzjt.com/dhwy/wxAuthor.html');
                var url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri='+redirect_uri+'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
                location.href = url;
            },
            goLogin(){
                let self = this;
                this.loadopen();
                this.config.mpost('/weixin/login/login', {code: this.code}).then(res=>{
                    this.loadclose();
                    if(res.data.code==='0'){
                        self.$message.success(res.data.message);
                        localStorage.setItem('mtoken', res.data.token)
                        self.$router.push('/mobile/address')
                    }else {
                        this.$message.error(this.config.mobileError(res.data));
                    }
                });
            },
        },
        mounted(){
            let obj = this.GetRequest()
            if(!obj.code){
                this.getAuthor()
            }else{
                this.code = obj.code;
                this.goLogin()
            }
        }
    }
</script>

<style scoped>

</style>
